package com.stk.doublebreak;

import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.text.Text;
import org.andengine.opengl.font.IFont;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

public class Scorer {
	private static final int LIFE_SGMENTS = 10;

	private Text text;
	private Text help;
	private Rectangle lifeOuter;
	private Rectangle lifeInner;
	private int width;
	private int height;
	private Rectangle life;
	private int maxLifeWidth;

	public Scorer (int width, int height, IFont font, VertexBufferObjectManager vbom){
		int border = height / 180;
		
		this.width = width;
		this.height = height;
		
		text = new Text(width/4, height/4 + border, font, "Level 0", "Level XXX".length(), vbom);
		text.setColor(Util.HexToRGB("#FFFFFF"));
		text.setAlpha(0f);
		
		help = new Text(width/6, (height/4) * 3, font, "Break all the TRIANGLES", "Break all the XXXXXXXXXXXXXXXXXXX".length(), vbom);
		help.setColor(Util.HexToRGB("#FFFFFF"));	
		help.setAlpha(0f);		
		
		lifeOuter = new Rectangle(width/2,0,width/2, height/10,vbom);
		lifeOuter.setColor(Util.HexToRGB("#BBBBBB"));
		
		lifeInner = new Rectangle(width/2 + border ,0 + border ,width/2 - border*2 , (height/10) - border*2,vbom);
		lifeInner.setColor(Util.HexToRGB("#111111"));
		
		life = new Rectangle(width/2 + border ,0 + border ,width/2 - border*2 , (height/10) - border*2,vbom);
		life.setColor(Util.HexToRGB("#33B5E5"));
		
		this.maxLifeWidth = width/2 - border*2;
	}
	
	public void updateOnNewLevel(int level, int color){
		text.reset();		
		text.setText("Level " + level);
		text.setScaleCenter(0, 0);
		text.registerEntityModifier(
				new SequenceEntityModifier(					
						new ParallelEntityModifier(
								new AlphaModifier(2, 0.0f, 1.0f),
								new ScaleModifier(0.1f, 0, (this.width/2) / text.getWidthScaled(), 0, (height/2) / text.getHeightScaled())
						),
						new AlphaModifier(4, 1.0f, 1.0f),
						new AlphaModifier(4, 1.0f, 0.0f)
				)
		);
		
		help.reset();		
		switch (color){
		case 0: help.setText("Break all the TRIANGLES");
				help.setColor(Util.HexToRGB("#CC0000"));
				break;
		case 1: help.setText("Break all the SQUARES");
				help.setColor(Util.HexToRGB("#FF8800"));
				break;
		case 2: help.setText("Break all the PENTAGONS");
				help.setColor(Util.HexToRGB("#669900"));
				break;
		case 3: help.setText("Break all the HEXAGONS");
				help.setColor(Util.HexToRGB("#9933CC"));
				break;
		}
		help.setScaleCenter(0, 0);
		help.registerEntityModifier(
				new SequenceEntityModifier(			
						new AlphaModifier(2, 0.0f, 0.0f),
						new ParallelEntityModifier(
								new AlphaModifier(3, 0.0f, 1.0f),
								new ScaleModifier(0.01f, 0, ((width/3)*2) / help.getWidthScaled(), 0, (height/8) / help.getHeightScaled())
						),
						new AlphaModifier(2, 1.0f, 1.0f),
						new AlphaModifier(3, 1.0f, 0.0f)
				)
		);
	}
	
	public void decreaseLife(){
		if (this.life.getWidth() - maxLifeWidth/ LIFE_SGMENTS > 0){
			this.life.setSize(this.life.getWidth() - maxLifeWidth/ LIFE_SGMENTS , life.getHeight());
		}		
	}
	
	public void increaseLife(){
		if (this.life.getWidth() + maxLifeWidth/ LIFE_SGMENTS <= maxLifeWidth){
			this.life.setSize(this.life.getWidth() + maxLifeWidth/ LIFE_SGMENTS , life.getHeight());
		}		
	}

	public Text getText() {
		return text;
	}
	
	public Text getHelp() {
		return help;
	}

	public Rectangle getLifeOuter() {
		return lifeOuter;
	}

	public Rectangle getLifeInner() {
		return lifeInner;
	}
	
	public Rectangle getLife() {
		return life;
	}

	public int getMaxLifeWidth() {
		return maxLifeWidth;
	}
	
	
}
